package com.xique.door.service.impl;


import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xique.common.core.utils.DateUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.door.bean.domain.BaseUser;
import com.xique.door.bean.domain.BaseUserV1;
import com.xique.door.mapper.BaseUserMapper;
import com.xique.door.service.IBaseUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户Service业务层处理
 *
 * @author caogq
 * @date 2021-12-10
 */
@Service
public class BaseUserServiceImpl extends ServiceImpl<BaseUserMapper, BaseUserV1> implements IBaseUserService
{
    @Autowired
    private BaseUserMapper baseUserMapper;

    /**
     * 查询用户
     *
     * @param id 用户ID
     * @return 用户
     */
    @Override
    public BaseUser selectBaseUserById(Long id)
    {
        return baseUserMapper.selectBaseUserById(id);
    }

    @Override
    public BaseUser selectBaseUserByLoginName(String loginName) {
        if (StringUtils.isEmpty(loginName)) {
            return null;
        }
        return baseUserMapper.selectBaseUserByLoginName(loginName);
    }

    @Override
    public List<BaseUserV1> selectBaseUserV1ByLoginName(String loginName) {
        QueryWrapper<BaseUserV1> condition = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(loginName)) {
            condition.eq("cellphone", loginName);
        }
        condition.eq("del_flag", 0);
        return baseUserMapper.selectList(condition);
    }

    @Override
    public List<BaseUserV1> selectBaseUserV1sByLoginName(String loginName) {
        QueryWrapper<BaseUserV1> condition = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(loginName)) {
            condition.eq("cellphone", loginName);
        }
        condition.eq("del_flag", 0);
        return baseUserMapper.selectList(condition);
    }

    @Override
    public BaseUserV1 selectBaseUserV1sCellphone(String cellphone) {
        QueryWrapper<BaseUserV1> condition = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(cellphone)) {
            condition.eq("cellphone", cellphone);
        }
        condition.eq("del_flag", 0);
        return baseUserMapper.selectOne(condition);
    }

    @Override
    public List<BaseUserV1> selectBaseUserV1ByOpenId(String openId) {
        QueryWrapper<BaseUserV1> condition = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(openId)) {
            condition.eq("wechat_openid", openId);
        }
        condition.eq("del_flag", 0);
        List<BaseUserV1> baseUserV1s = baseUserMapper.selectList(condition);
        if (baseUserV1s.size()>1){
            log.warn("[门禁小程序]登录，相同openid存在多个用户,openid：" + openId);
        }
        return baseUserV1s;
    }

    @Override
    public BaseUserV1 selectBaseUserV1ByUnionId(String unionId) {
        if (StrUtil.isEmpty(unionId)) {
            return null;
        }
        LambdaQueryWrapper<BaseUserV1> condition = Wrappers.lambdaQuery();
        condition.eq(BaseUserV1::getWechatUnionid, unionId);
        condition.eq(BaseUserV1::getDelFlag, 0);
        return baseUserMapper.selectOne(condition);
    }

    /**
     * 查询用户列表
     *
     * @param baseUser 用户
     * @return 用户
     */
    @Override
    public List<BaseUser> selectBaseUserList(BaseUser baseUser)
    {
        return baseUserMapper.selectBaseUserList(baseUser);
    }

    /**
     * 新增用户
     *
     * @param baseUser 用户
     * @return 结果
     */
    @Override
    public int insertBaseUser(BaseUser baseUser)
    {
        baseUser.setCreateTime(DateUtils.getNowDate());
        return baseUserMapper.insertBaseUser(baseUser);
    }

    /**
     * 修改用户
     *
     * @param baseUser 用户
     * @return 结果
     */
    @Override
    public int updateBaseUser(BaseUser baseUser)
    {
        baseUser.setUpdateTime(DateUtils.getNowDate());
        return baseUserMapper.updateBaseUser(baseUser);
    }

    /**
     * 批量删除用户
     *
     * @param ids 需要删除的用户ID
     * @return 结果
     */
    @Override
    public int deleteBaseUserByIds(Long[] ids)
    {
        return baseUserMapper.deleteBaseUserByIds(ids);
    }

    /**
     * 删除用户信息
     *
     * @param id 用户ID
     * @return 结果
     */
    @Override
    public int deleteBaseUserById(Long id)
    {
        return baseUserMapper.deleteBaseUserById(id);
    }

    @Override
    public List<BaseUser> selectBaseUserByPhone(String phone) {
        BaseUser baseUser = new BaseUser();
        baseUser.setCellphone(phone);
        List<BaseUser> baseUsers = selectBaseUserList(baseUser);
        if (baseUsers.size()>1){
            log.error("[门禁服务查询baseUser]相同手机号查出多个用户，手机号："+ phone);
        }
        return baseUsers;
    }

    @Override
    public List<BaseUserV1> selectBaseUserByParkMiniOpenIdAndPhone(String openId, String phone) {
        if (StrUtil.isEmpty(openId) || StrUtil.isEmpty(phone)) {
            return null;
        }
        LambdaQueryWrapper<BaseUserV1> condition = Wrappers.lambdaQuery();
        condition.eq(BaseUserV1::getWechatOpenid, openId);
        condition.eq(BaseUserV1::getCellphone, phone);
        condition.orderByDesc(BaseUserV1::getId);
        List<BaseUserV1> baseUsers = baseUserMapper.selectList(condition);
        if (baseUsers.size()>1){
            log.error("[门禁小程序]登录，相同openId+手机号存在多个用户，open："+openId+",手机号："+phone);
        }
        return baseUsers;
    }
}
